﻿using SSC.ViewModels;
using SSC.Views;
using System.Windows;

namespace SSC
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private readonly MainViewModel _viewModel;
        public MainWindow()
        {
            InitializeComponent();
            _viewModel = new MainViewModel(this);
            DataContext = new MainViewModel(this);
            this.Loaded += MainWindow_Loaded;
        }
        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            CheckMaintenanceOverdue();
        }
        private void CheckMaintenanceOverdue()
        {
            var overdueItems = _viewModel.GetOverdueItems();

            if (overdueItems.Count > 0)
            {
                var dlg = new wdMaintenanceCheck(overdueItems);
                dlg.Owner = this; // this 指已经显示的主窗口，合法
                dlg.ShowDialog();
            }
        }
        private void LoginMenuItem_Click(object sender, RoutedEventArgs e)
        {
            // 实例化并显示登录窗体
            var loginWindow = new Views.wdLogin();
            loginWindow.ShowDialog();
        }

        private void UserManagementMenuItem_Click(object sender, RoutedEventArgs e)
        {
            var window = new UserWindow();
            window.ShowDialog(); 
        }


        private void PartMenuItem_Click(object sender, RoutedEventArgs e)
        {
            var partWindow = new Views.wdPart();
            partWindow.ShowDialog();
        }

        private void DataMonitor_Click(object sender, RoutedEventArgs e)
        {
            /*ContentArea.Content = new ucDataMonitor();*/
        }
        private void MenuItem_OperationLog_Click(object sender, RoutedEventArgs e)
        {
            var recordWindow = new SSC.Views.wdRecord();
            recordWindow.Show(); 
        }
        private MaintenanceWindow? _maintenanceWindow;
        private void MaintenanceMenuItem_Click(object sender, RoutedEventArgs e)
        {
            var window = new MaintenanceWindow();
            window.Show();
        }
        private void FeedMenuItem_Click(object sender, RoutedEventArgs e)
        {
            var feedWindow = new wdFeed();  // 假设你已经创建 wdFeed.xaml
            feedWindow.Owner = this;
            feedWindow.ShowDialog();
        }
        private void AlarmLogMenuItem_Click(object sender, RoutedEventArgs e)
        {
            var win = new wdAlarmLog();  
            win.ShowDialog(); 
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            var result = MessageBox.Show("确定要退出系统吗？", "确认退出", MessageBoxButton.YesNo, MessageBoxImage.Question);
            // 如果用户选择了 "No"，则取消关闭操作
            if (result != MessageBoxResult.Yes)
            {
                e.Cancel = true;
            }
            else
            {
                // 在这里可以添加任何需要在关闭前执行的清理操作
                // 例如：保存设置、断开数据库连接等
            }
        }
    }
}